<?php

//search for an item, maybe in certain category
//returns array of array(item id, item name, category name, seller id, seller name, item cost)
function searchItem($string, $category_id = false, $start = 0) {
	$string = escape($string);
	
	$query = "SELECT items.id, items.name, categories.name, items.seller_id, users.name, items.cost FROM items LEFT JOIN categories ON categories.id = items.category_id LEFT JOIN users ON users.id = items.seller_id WHERE ( items.name LIKE '%$string%' OR items.description LIKE '%$string%' ) AND items.status = '0'";
	
	if($category_id !== false) {
		$query .= " AND items.category_id = '" . escape($category_id) . "'";
	}
	
	$query .= " ORDER BY items.id DESC LIMIT $start, 20";
	$result = mysql_query($query);
	
	$items = array();
	
	while($row = mysql_fetch_array($result)) {
		$items[] = array($row[0], $row[1], $row[2], $row[3], $row[4], $row[5]);
	}
	
	return $items;
}

//find what items a certain user is selling
//returns array of array(item id, item name, category name, seller id, seller name, item cost)
function getSellerItems($seller_id, $start = 0) {
	$seller_id = escape($seller_id);
	
	$result = mysql_query("SELECT items.id, items.name, categories.name, items.seller_id, users.name, items.cost FROM items LEFT JOIN categories ON categories.id = items.category_id LEFT JOIN users ON users.id = items.seller_id WHERE items.seller_id = '$seller_id' AND items.status = '0' ORDER BY items.id DESC LIMIT $start, 20");
	
	$items = array();
	
	while($row = mysql_fetch_array($result)) {
		$items[] = array($row[0], $row[1], $row[2], $row[3], $row[4], $row[5]);
	}
	
	return $items;
}

//returns false if type is invalid or user not found
//otherwise returns user id
function getUser($string, $type = "email") {
	if($type != 'email' && $type != 'name') {
		return false;
	}
	
	$string = escape($string);
	$result = mysql_query("SELECT users.id WHERE users.$type = '$string'");
	
	if($row = mysql_fetch_array($result)) {
		return $row[0];
	} else {
		return false;
	}
}

?>
